package com.huitone.gddw.service.impl.worktable;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.Sensitive;
import com.huitone.gddw.dao.worktable.paramconfigmanage.ISensitiveDao;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.service.ISensitiveService;
import com.huitone.gddw.service.dto.SensitiveDTO;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 敏感词过滤 接口实现类
 * @author wuluodan
 *
 */
@Service
public class SensitiveService implements ISensitiveService {

	@Autowired
	private ISensitiveDao sensitiveDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public QueryPage<SensitiveDTO> selectPage(QueryPage<Sensitive> page) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("senName", page.getParams().get("senName"));
		map.put("start", (page.getPageNo() - 1) * page.getPageSize() + 1);
		map.put("end", page.getPageNo() * page.getPageSize());
		List<SensitiveDTO> result = sensitiveDao.selectPage(map);
		int totalCount = sensitiveDao.selectCount(map);
		
		QueryPage<SensitiveDTO> queryPage = new QueryPage<SensitiveDTO>();
		queryPage.setResult(result);
		queryPage.setPageNo(page.getPageNo());
		queryPage.setPageSize(page.getPageSize());
		queryPage.setTotalCount(totalCount);
		return queryPage;
	}

	@Override
	public Sensitive selectByPrimaryKey(String senId) {
		return sensitiveDao.selectByPrimaryKey(senId);
	}

	@Override
	public SensitiveDTO toUpdate(String senId) {
		return new SensitiveDTO(this.selectByPrimaryKey(senId));
	}

	@Override
	public void changeStatus(Sensitive entity) {
		this.update(entity);
	}

	@Override
	public void save(Sensitive entity) {
		entity.setInsertTime(new Date());
		entity.setInsertStaffId(SysLoginUtil.getLoginUserId());
		entity.setLastModTime(entity.getInsertTime());
		entity.setLastModStaffId(entity.getInsertStaffId());
		sensitiveDao.save(entity);
		
		this.updateParamRec("保存敏感词");
	}

	@Override
	public void update(Sensitive entity) {
		entity.setLastModTime(new Date());
		entity.setLastModStaffId(SysLoginUtil.getLoginUserId());
		sensitiveDao.updateByPrimaryKey(entity);
		
		this.updateParamRec("更新敏感词");
	}

	@Override
	public void delete(String senId) {
		sensitiveDao.deleteByPrimaryKey(senId);
		this.updateParamRec("删除敏感词");
	}

	@Override
	public List<SensitiveDTO> getRecord(Map<String, Object> params) {
		List<SensitiveDTO> list = sensitiveDao.getRecord(params);
		return list;
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.SENSITIVE, desc);
	}

	@Override
	public List<Sensitive> getBySenName(String senName) {
		return sensitiveDao.getBySenName(senName);
	}

}
